package com.example.mobile2021606128wje0408;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private CheckBox chkboxMainYinqing;
    private CheckBox chkboxMainBoil;
    private CheckBox chkboxMainFangxiangpan;
    private Button btnMainBuy;
    private TextView tvMainTip;
    private TextView tvMainResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //复选框中选中状态的变化
        chkboxMainYinqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvMainResult.setText("选中："+buttonView.getText().toString());
                }else {
                    tvMainResult.setText("取消选中："+buttonView.getText().toString());
                }
            }
        });
        chkboxMainBoil.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvMainResult.setText("选中："+buttonView.getText().toString());
                }else {
                    tvMainResult.setText("取消选中："+buttonView.getText().toString());
                }
            }
        });
        chkboxMainFangxiangpan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    tvMainResult.setText("选中："+buttonView.getText().toString());
                }else {
                    tvMainResult.setText("取消选中："+buttonView.getText().toString());
                }
            }
        });
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //以此判断复选框的选中状态
                String msg="选中";
                if (chkboxMainYinqing.isChecked()){
                    msg+="\n"+chkboxMainYinqing.getText().toString();
                }if (chkboxMainBoil.isChecked()){
                    msg+="\n"+chkboxMainBoil.getText().toString();
                }if (chkboxMainFangxiangpan.isChecked()){
                    msg+="\n"+chkboxMainFangxiangpan.getText().toString();
                }
                tvMainResult.setText(msg);
            }
        });


    }

    private void initView() {
        chkboxMainYinqing = (CheckBox) findViewById(R.id.chkbox_Main_Yinqing);
        chkboxMainBoil = (CheckBox) findViewById(R.id.chkbox_Main_Boil);
        chkboxMainFangxiangpan = (CheckBox) findViewById(R.id.chkbox_Main_Fangxiangpan);
        btnMainBuy = (Button) findViewById(R.id.btn_main_buy);
        tvMainTip = (TextView) findViewById(R.id.tv_main_Tip);
        tvMainResult = (TextView) findViewById(R.id.tv_main_Result);
    }
}